#include<iostream>
#include<unistd.h>
#include<cstdio>
#include<cstdlib>
#include<sys/types.h>
#include<sys/wait.h>
using namespace std;
int main()
{
    putenv("MYVALVAL=123456");
    extern char**envion;//申明
    pid_t id=fork();
    while(id==0)
    {
        printf("before:I am a process,pid:%d,ppid:%d\n",getpid(),getppid());
        //char*const myargv[]={
        //    "-ls","-l","-a",NULL
        //};
        //sleep(2);
        //标准写法
        //可执行文件的路径  如何执行（各种选项）
        // execl("/usr/bin/ls","-ls","-a","-l",NULL);   
        //execlp("ls","-ls","-a","-l",NULL);   
        //execl("/usr/bin/top","top",NULL);
        //execv("/usr/bin/ls",myargv);
        //execl("./otherExe","otherExe",NULL);
        //execl("/usr/bin/bash","bash","test.sh",NULL);//运行脚本文件
        char*const myargv[]={
            "otherExe","-a","-b","-c",NULL
        };
        //execv("./otherExe",myargv);
        //environ 为父进程的环境变量
        char *const myenv[]={
            "MYVAL=111","MYPATH=/use/bin/XXX",NULL
        };
        execle("./otherExe","otherExe","-a","-b","-w",NULL,myenv);
        //char *const myenv[]={
        //    "MYVAL=111","MYPATH=/use/bin/XXX",NULL
        //};
        //execle("./otherExe","otherExe","-a","-b","-w",NULL,myenv);
        printf("after:I am a process,pid:%d,ppid:%d\n",getpid(),getppid());
        //只要程序走到这，说明替换不成功，并返回退出码1
        exit(1);    
    }
    int ret=waitpid(id,NULL,0);
    if(ret>0)
    {
        printf("wait success,father pid:%d,ret id:%d\n",getpid(),ret);
    }
    sleep(3);
    return 0;
}
